package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.mapper.MygPaintingMapper;
import com.rongke.model.ExperienceGoods;
import com.rongke.model.Goods;
import com.rongke.model.MygPainting;
import com.rongke.service.GoodsService;
import com.rongke.service.MygPaintingService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.*;

/**
 * @MygPaintingController
 * @蜜雅阁字画Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/mygPainting")
@Transactional
@CrossOrigin(origins = "*",maxAge = 3600)
public class MygPaintingController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private MygPaintingService mygPaintingService;
    @Autowired
    private GoodsService goodsService;

    /**
     * @添加蜜雅阁字画
     * @param mygPainting
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addMygPainting(@RequestBody MygPainting mygPainting){
        log.debug("添加蜜雅阁字画");
        mygPainting.setGmtDatetime(new Date());
        mygPaintingService.insert(mygPainting);
        return JsonResp.ok(mygPainting);
    }

    /**
     * @批量添加体验众测
     * @param mygPaintings
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/addBatch", method = RequestMethod.POST)
    public JsonResp addBatch(@RequestBody List<MygPainting> mygPaintings ){
        log.debug("批量添加到字画列表");
        for (MygPainting mp:mygPaintings){
            mp.setGmtDatetime(new Date());
        }
        mygPaintingService.insertBatch(mygPaintings);
        return JsonResp.ok("");
    }

    /**
     * @修改蜜雅阁字画
     * @param mygPainting
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateMygPainting(@RequestBody MygPainting mygPainting){
        log.debug("修改蜜雅阁字画");
        mygPaintingService.updateById(mygPainting);
        return JsonResp.ok(mygPainting);
    }
    /**
     * @根据id查找蜜雅阁字画
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectMygPainting(Long id){
        log.debug("查找蜜雅阁字画");
        MygPainting mygPainting = mygPaintingService.selectById(id);
        return JsonResp.ok(mygPainting);
    }

    /**
     * @根据条件查找蜜雅阁字画
     * @param mygPainting
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectMygPaintingByCondition(@RequestBody MygPainting mygPainting){
        log.debug("根据条件查找蜜雅阁字画");
        if(mygPainting==null)
            return JsonResp.toFail("参数不能为空");
        List<MygPainting> mygPaintingList=mygPaintingService.selectByCondition(mygPainting);
        return JsonResp.ok(mygPaintingList);
    }

    /**
     * @分页查找蜜雅阁字画商品
     * @param page
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectPage", method = RequestMethod.GET)
    public JsonResp selectPaintingGoods(Page page){
        log.debug("分页查找蜜雅阁字画商品");
        Map map=new HashMap();
        Integer startIndex=(page.getCurrent()-1)*page.getSize();
        map.put("startIndex",startIndex);
        map.put("size",page.getSize());
        List<MygPainting> origiGoodsPage = mygPaintingService.selectPaintingGoodsList(map);
        List<MygPainting> goodsList=new ArrayList<>();
        for(MygPainting eg:origiGoodsPage){
            if(eg.getPicUrl()!=null) {
                goodsList.add(eg);
            }
        }
        int total=mygPaintingService.selectCount(null);
        total=goodsList.size();
        page.setRecords(goodsList);
        page.setTotal(total);
        return JsonResp.ok(page);
    }
    /**
     * 获取蜜雅阁待添加的字画商品集合（交给web客户端分页）
     * @param goodsType
     * @return
     */
    @RequestMapping(value = "/getNotGroupedGoodsList",method = RequestMethod.GET)
    public JsonResp getNotGroupedGoodsList(int goodsType) {
        log.debug("获取蜜雅阁待添加的字画商品集合");
        EntityWrapper<Goods> ew = new EntityWrapper<>();
        //goods_status 商品状态 1代表只上传未发布 2代表正式发布上架 3 下架
        ew.eq("goods_status", 2).eq("goods_type", goodsType);
        List<Goods> goodsList = goodsService.selectList(ew);
        //查询已有的商品
        List<MygPainting> Goodses = mygPaintingService.selectPaintingGoodsList(new HashMap());
        if (!Goodses.isEmpty()) {
            for (int i = 0, y = Goodses.size(); i < y; i++) {
                Goods goods =Goodses.get(i).getGoods();
                if (goodsList.contains(goods)) {
                    goodsList.remove(goods);
                }
            }
        }
        return JsonResp.ok(goodsList);
    }

    /**
     * @后台分页查找字画商品
     * @param page
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectPageAdmin", method = RequestMethod.GET)
    public JsonResp selectMygPaingtingAdmin(Page page){
        log.debug("后台分页查找字画商品");
        Map map=new HashMap();
        Integer startIndex=(page.getCurrent()-1)*page.getSize();
        map.put("startIndex",startIndex);
        map.put("size",page.getSize());
        List<MygPainting> goodsPage = mygPaintingService.selectPaintingGoodsList(map);
        int total=mygPaintingService.selectCount(null);
        page.setRecords(goodsPage);
        page.setTotal(total);
        return JsonResp.ok(page);
    }

    /**
     * @删除
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/deleteMygPainting", method = RequestMethod.GET)
    public JsonResp deleteMygPainting(Long id){
        log.debug("删除");
        mygPaintingService.deleteById(id);
        return JsonResp.ok();
    }
}
